<?php
/**
 *  列表信息控制器
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\Listinfo;

class Weblistinfo extends AdminBase
{
    private $pid = 1;
    private $navcat_list;
    protected function _initialize()
    {
        parent::_initialize();
        $this->navcat_list = model('nav')->where(['pid'=>$this->pid])->column('name_cn','id');//'model'=>1开启列表/单页筛选
        $this->assign('navcat_list',$this->navcat_list);
    }
    public function index()
    {
        return $this->fetch('index');
    }

    public function getLists()
    {
        if($this->request->isAjax()) {
            $m = new Listinfo;
            return $m->getLists();
        }
    }
    public function edit()
    {
        $rows = Listinfo::get(input('id/d'));
        session('redirect_url',$_SERVER['HTTP_REFERER']);   //记住编辑前的url，修改后调回
        return $this->fetch('edit',['rows'=>$rows]);
    }

    public function save()
    {
        if($this->request->isAjax()){
            $m = new Listinfo();
            return $m->_save();
        }
    }

    //更新'0'或'1'
    public function flag($id)
    {
        if ($this->request->isAjax()) {
            $m = new Listinfo;     $data = input('post.');
            $res = $m->allowField(['is_show','is_top','is_index'])->save($data,['id'=>$id]); //失败返回false
            if($res !== false) $this->success('更新成功');
            else $this->error('请重试');
        }
    }
    //软删除
    public function delete($id = 0, $ids = [])
    {
        if($this->request->isAjax()){
            $id = $ids ? $ids : $id;
            $m = new Listinfo;
            return $m->_delete($id);
        }
    }
}